package com.bq.gametrade.view.popwindow;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bq.gametrade.R;
import com.bq.gametrade.bean.BankCardListDetails;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Feel on 2017/2/23.
 */

public class BankCardListPop extends PopupWindow {

    private Activity activity;
    private ArrayList<BankCardListDetails> list;
    private String bankCardNo4 ;
    private LinearLayout layoutBankCardList;
    private ListView listView;
    private TextView tvDismiss;
    private BankCardNoSelectAdapter adapter;

    public interface OnGetBankCardNoListener {
        void getBankCardNo(int position);
    }
    public BankCardListPop(Activity activity, ArrayList<BankCardListDetails> list,String no4){
        super(activity);
        this.activity = activity;
        this.list = list;
        this.bankCardNo4 = no4;
        View view = LayoutInflater.from(activity).inflate(R.layout.pop_selet_bankcard, null);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        this.setContentView(view);
//        this.setAnimationStyle(R.style.PayPasswordPopAnimation);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
        initView(view);

    }

    private void initView(View view){
        listView = (ListView) view.findViewById(R.id.listView);
        adapter = new BankCardNoSelectAdapter();
        listView.setAdapter(adapter);
        layoutBankCardList = (LinearLayout) view.findViewById(R.id.layout_bankCardList);
        layoutBankCardList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setBankCardNoListener(final OnGetBankCardNoListener listener){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectionState(position);
                adapter.notifyDataSetChanged();
                listener.getBankCardNo(position);
                dismiss();
            }
        });

    }



    class BankCardNoSelectAdapter extends BaseAdapter {
        private int selectedPosition=0;
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        //这句是把listview的点击position,传递过来
        public void selectionState(int position) {
            selectedPosition = position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(activity).inflate(R.layout.item_select_bankcardno_listview, null);
//                viewHolder.tvBankName = (TextView) convertView.findViewById(R.id.tv_bankName);
                viewHolder.tvBanCard = (TextView) convertView.findViewById(R.id.tv_bankCard);
                viewHolder.ivIsSelected = (ImageView) convertView.findViewById(R.id.iv_isSelected);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if (list.get(position).card_num.substring(list.get(position).card_num.length()-4,list.get(position).card_num.length()).equals(bankCardNo4)){
                viewHolder.ivIsSelected.setVisibility(View.VISIBLE);
                viewHolder.tvBanCard.setTextColor(activity.getResources().getColor(R.color.color_yellow));
            }else {
                viewHolder.ivIsSelected.setVisibility(View.INVISIBLE);
                viewHolder.tvBanCard.setTextColor(activity.getResources().getColor(R.color.black));

            }
//            viewHolder.tvBankName.setText(list.get(position).bank_name);
            viewHolder.tvBanCard.setText(list.get(position).bank_name+": 尾号 "+list.get(position).card_num.substring(list.get(position).card_num.length()-4,list.get(position).card_num.length()));

            return convertView;
        }

        class ViewHolder {
            TextView tvBankName;
            TextView tvBanCard;
            ImageView ivIsSelected;


        }
    }
}
